<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class NewsController extends AppController {

    var $layout = 'default';
    var $scripts = array('jquery-1.7.1.js', 'jcarousellite_1.0.1.js', 'slideshowNews.js', 'common.js');
    var $uses = array(
        'News','Catnews', 'Comment'
    );
    var $components = array(
        'Security'
    );
    function index($page = 1) {
        $this->set('title_for_layout', 'Tin tức - thời trang red');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : $page;
        $conditions = array(
            'News.published' => 1,
        );
        $this->paginate = array(
            'fields' => array('News.created, News.id, News.title, News.alias, News.thumbnail, News.short_content'),
            'conditions' => $conditions,
            'limit' => 9,
            'order' => array(
                'News.id' => 'DESC'
            ),
            'recursive' =>  -1

        );
        

        $data = $this->paginate('News');
        $this->set('total', $this->params['paging']['News']['count']);
        $this->set('data', $data);
        $this->set('news_image_url_path', Configure::read('news_image_url_path'));
        $this->set('start_page', $page);
    }
    function cat_news($id = null, $slug = null) {
        $cat_name   =   $this->Catnews->field('title', array('Catnews.id'=>$id));
                
        $this->set('title_for_layout', "Tin tức - $cat_name - thời trang red");
        
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        if (!isset($id)) {
            $id = isset($this->params['id']) ? $this->params['id'] : 1;
        }
        $conditions = array(
            'News.published' => 1,
            'News.cat_id' => $id
        );
        $this->paginate = array(
            'fields' => array('News.id, News.created, News.title, News.alias, News.thumbnail, News.short_content'),
            'conditions' => $conditions,
            'limit' => 9,
            'order' => array(
                'News.id' => 'DESC'
            ),
            'recursive' =>  -1
        );

        $data = $this->paginate('News');
        $this->set('total', $this->params['paging']['News']['count']);
        $this->set('data', $data);
        $this->set('cat_name', $cat_name);
        $this->set('news_image_url_path', Configure::read('news_image_url_path'));
        $this->set('start_page', $page);
    }
    function detail($id){
        $content = $this->News->read(array('id','alias','title', 'content',"DATE_FORMAT(`News`.`created`, '%d-%m-%Y') AS created", 'cat_id'), $id);
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $conditions = array(
            'Comment.published' => 1,
            'Comment.module' => 'news',
            'Comment.module_unique_id' => $id,
        );
        $this->paginate = array(
            'fields' => array("DATE_FORMAT(Comment.created,'%d-%m-%Y %h:%h %p') AS 'Comment.created', Comment.id, Comment.subject, Comment.content, User.avatar, User.id, User.name"),
            'conditions' => $conditions,
            'limit' => 10,
            'order' => array(
                'Comment.id' => 'DESC'
            ),
        );
        $data_comments = $this->paginate('Comment');
        $this->set('total', $this->params['paging']['Comment']['count']);
        $this->set('data_comments', $data_comments);

        
        $this->set('request_id', $id);
        $this->set('title_for_layout', "Đọc tin &gt;&gt;{$content['News']['title']}- Thời Trang RED");
        $this->set('content', $content);
        $this->set('other_news', $this->News->getOtherNews($content['News']['cat_id'],$id));
    }
}

?>
